package com.uatair.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.uatair.common.annotation.Log;
import com.uatair.common.core.controller.BaseController;
import com.uatair.common.core.domain.AjaxResult;
import com.uatair.common.core.page.TableDataInfo;
import com.uatair.common.enums.BusinessType;
import com.uatair.common.utils.StringUtils;
import com.uatair.common.utils.poi.ExcelUtil;
import com.uatair.system.domain.TpProjectTask;
import com.uatair.system.service.TpProjectTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 历史任务信息Controller
 *
 * @author uatair
 * @date 2025-06-23
 */
@RestController
@RequestMapping("/system/task")
public class TpProjectTaskController extends BaseController {
    @Autowired
    private TpProjectTaskService tpProjectTaskService;

    private static final Logger log = LoggerFactory.getLogger(TpProjectTaskController.class);
    /**
     * 查询历史任务信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TpProjectTask tpProjectTask,
                              @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        // 初始化分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 用自定义的带 JOIN 的方法
        List<TpProjectTask> list = tpProjectTaskService.selectTpProjectTaskList(tpProjectTask);

        // 使用现有的 getDataTable 方法
        return getDataTable(list);
    }

    /**
     * 导出历史任务信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "历史任务信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TpProjectTask tpHistoryTask) {
        QueryWrapper<TpProjectTask> queryWrapper = new QueryWrapper<>();
        if (tpHistoryTask != null) {
            queryWrapper.eq(StringUtils.isNotEmpty(tpHistoryTask.getName()), "name", tpHistoryTask.getName());
            // 其他字段的条件可以类似添加
        }
        List<TpProjectTask> list = tpProjectTaskService.list(queryWrapper);
        ExcelUtil<TpProjectTask> util = new ExcelUtil<TpProjectTask>(TpProjectTask.class);
        util.exportExcel(response, list, "历史任务信息数据");
    }

    /**
     * 获取历史任务信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId) {
        TpProjectTask task = tpProjectTaskService.getOne(new QueryWrapper<TpProjectTask>().eq("task_id", taskId));
        return success(task);
    }

    /**
     * 新增历史任务信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "历史任务信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TpProjectTask tpHistoryTask) {
        boolean result = tpProjectTaskService.save(tpHistoryTask);
        return toAjax(result);
    }

    /**
     * 修改历史任务信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "历史任务信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TpProjectTask tpHistoryTask) {
        boolean result = tpProjectTaskService.updateById(tpHistoryTask);
        return toAjax(result);
    }

    /**
     * 删除历史任务信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "项目任务信息", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody List<String> taskIds) {
        boolean result = tpProjectTaskService.removeByIds(taskIds);
        return toAjax(result);
    }

    /**
     * 修改历史任务状态
     */
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "历史任务信息", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TpProjectTask task) {
        UpdateWrapper<TpProjectTask> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("task_id", task.getTaskId())              // 主键条件
                .set("execute_status", task.getExecuteStatus()); // 要更新的字段

        boolean result = tpProjectTaskService.update(updateWrapper);
        return toAjax(result);
    }
}

